package gl.view.widget;

import gl.android.biyiqing.R;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

public class HorizonTextWheel extends FrameLayout {

	private ViewPager vp;
	private String[] ss;

	public HorizonTextWheel(Context context, AttributeSet attrs,
			int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		init();
	}

	public HorizonTextWheel(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	public HorizonTextWheel(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	public int getSelected() {
		return vp==null?0:vp.getCurrentItem();

	}
	public void setTextArray(String[] array) {
		if (array == null) {
			return;
		}

		LayoutInflater lf = (LayoutInflater) getContext().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		removeAllViews();
		addView(lf.inflate(R.layout.wheel_h, null));
		vp = (ViewPager) findViewById(R.id.pager);
		ss = array;
		vp.setAdapter(new PagerAdapter() {

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return false;
			}

			@Override
			public Object instantiateItem(View container, int position) {
				final TextView textView = new TextView(getContext());
				return textView;
			}

			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager) container).removeView((View) object);
			}

			@Override
			public CharSequence getPageTitle(int position) {
				SpannableStringBuilder ssb = new SpannableStringBuilder(
						ss[position]);
				ForegroundColorSpan fcs = new ForegroundColorSpan(Color.WHITE);// 字体颜色设置为绿色
				ssb.setSpan(fcs, 0, ssb.length(),
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);// 设置字体颜色
				ssb.setSpan(new RelativeSizeSpan(0.8f), 0, ssb.length(),
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				return ssb;
			}

			@Override
			public int getCount() {
				return ss.length;
			}
		});
	}

	private void init() {
	}
}
